package com.pubinfo.passbook.api.controller.contract;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.pubinfo.passbook.api.eip.UserInfoService;
import com.pubinfo.passbook.common.exception.ServiceException;
import com.pubinfo.passbook.common.model.dto.contract.ContractIdParam;
import com.pubinfo.passbook.common.model.dto.contract.TaxParam;
import com.pubinfo.passbook.common.model.dto.witeoff.CompensateForRemoveDTO;
import com.pubinfo.passbook.common.model.vo.base.BusinessErrorCode;
import com.pubinfo.passbook.common.model.vo.base.JsonResult;
import com.pubinfo.passbook.common.model.vo.contract.*;
import com.pubinfo.passbook.common.service.contract.TContractBaseService;
import com.pubinfo.passbook.common.service.contract.TContractInvoiceHeaderService;
import com.pubinfo.passbook.common.service.contract.TContractWriteoffRelationService;
import com.pubinfo.passbook.common.service.mss.CompensateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.DocumentException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@Api(tags = "QGPB-收益管理(结转)")
@RequestMapping(value = "earnCarryForward")
public class EarningCarryForwardController {

    @Resource
    private TContractBaseService contractBaseService;
    @Resource
    private TContractWriteoffRelationService contractWriteoffRelationService;
    @Resource
    private CompensateService compensateService;
    @Resource
    private TContractInvoiceHeaderService invoiceHeaderService;
    @Resource
    private UserInfoService userInfoService;

    @PostMapping("contractDetail")
    @ApiOperation(value = "赔补款信息")
    public JsonResult<ContractDetailVo> detail(@RequestBody ContractIdParam param) {
        return JsonResult.success(contractBaseService.contractDetail(param));
    }

    @PostMapping("wbsList")
    @ApiOperation(value = "WBS成本信息")
    public JsonResult<List<WBSInfoVo>> wbsList(@RequestBody ContractIdParam param) {
        return contractWriteoffRelationService.wbsList(param);
    }

    @PostMapping("incomeList")
    @ApiOperation(value = "收入结转挑对明细")
    public JsonResult<List<IncomeInfoVo>> incomeList(@RequestBody ContractIdParam param) {
        return contractWriteoffRelationService.incomeList(param);
    }

    @GetMapping("compensateList")
    @ApiOperation(value = "生成报账单页面")
    public JsonResult<CompensateBaseVo> compensateList(Integer type) {
        if (Objects.isNull(type))
            throw new ServiceException(BusinessErrorCode.PARAM_ERROR);
        return contractWriteoffRelationService.compensateList(userInfoService.getCurrentAcct(),type);
    }

    @PostMapping("compensateCreate")
    @ApiOperation(value = "结转报账接口")
    public JsonResult<String> pick(@RequestBody @Valid CompensateForRemoveDTO dto) throws DocumentException {
        return compensateService.compensateForRemove(dto, true);
    }

    @PostMapping("taxList")
    @ApiOperation(value = "税金计提列表")
    public JsonResult<IPage<TaxListVo>> taxList(@RequestBody TaxParam param) {
        return JsonResult.success(invoiceHeaderService.taxList(param));

    }

    @PostMapping("taxProvision")
    @ApiOperation(value = "税金计提报账接口")
    public JsonResult<String> taxProvision(@RequestBody @Valid CompensateForRemoveDTO dto) throws DocumentException {
        return compensateService.compensateForRemove(dto, true);
    }

}
